home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WVEC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-07  |  8.4 KB  |  467 lines

  1. #include "VecLocal.h"
  2. #pragma hdrstop
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5.  
  6. //.parse
  7.  
  8. BitSetRef::BitSetRef(Byte* BP, int BitOffset)
  9.   {
  10.     P=BP+size_t(BitOffset/8);
  11.     M=Byte(1<<int(BitOffset%8));
  12.   }
  13.  
  14. //.parse
  15.  
  16. ByteVector::ByteVector(File& F)
  17.   {
  18.     Extra=DefaultVectorExtra;
  19.     if (F.CurPos()>F.Size())
  20.       {
  21.         Len=0;
  22.         Alloc=Extra;
  23.         P = VecNew(Alloc);
  24.       }
  25.     else
  26.       {
  27.         F.ReadThing(Len);
  28.         if (Len)
  29.           {
  30.             Alloc=Len+Extra;
  31.             P =VecNew(Alloc);
  32.             if (Len)
  33.               {
  34.                 F.Read(P,int(Len));
  35.               }
  36.           }
  37.       }
  38.   }
  39.  
  40. //.parse
  41.  
  42. long ByteVector::Sum()
  43.   {
  44.     long I;
  45.     long A=0;
  46.     For(I,Len) A+=*PtrInc(P,I);
  47.     return A;
  48.   }
  49.  
  50. //.parse
  51.  
  52. long ByteVector::Index(Byte SearchByte, long StartIndex)
  53.   {
  54.     if (StartIndex>=Len) return(NotFound);
  55.     long I=StartIndex;
  56.     while ((I<Len) && ((*PtrInc(P,I))!=SearchByte)) I++;
  57.     if (I==Len) return(NotFound);
  58.     else return(I);
  59.   }
  60.  
  61. //.parse
  62.  
  63. ByteVector ByteVector::From(long Index)
  64.   {
  65.     ByteVector BV;
  66.     if (Len>Index) BV=At(Index,Len-Index);
  67.     return BV;
  68.   }
  69.  
  70. //.parse
  71.  
  72. ByteVector ByteVector::After(long Index)
  73.   {
  74.     ByteVector BV;
  75.     if (Len>(Index+1)) BV=At(Index+1,Len-Index-1);
  76.     return BV;
  77.   }
  78.  
  79. //.parse
  80.  
  81. Bool ByteVector::operator==(const ByteVector& B)
  82.   {
  83.     if (Len != B.Len) return False;
  84.     if (P==B.P) return True;
  85.     long I;
  86.     For(I,Len)
  87.         if ((*PtrInc(P,I))!=(*PtrInc(B.P,I))) return False;
  88.     return True;
  89.   }
  90.  
  91. //.parse
  92.  
  93. void ByteVector::Delete(long Index,long Length)
  94.   {
  95.     if (Length<1) return;
  96.     if (Index<0) Index=0;
  97.     if (Index+Length>=Len) Len=Index;
  98.     else
  99.       {
  100.         Len-=Length;
  101.         long I;
  102.         for(I=Index;I<Len;I++) *PtrInc(P,I)=*PtrInc(P,I+Length);
  103.       }
  104.   }
  105.  
  106. //.parse
  107.  
  108. void ByteVector::Clip(long NewSize)
  109.   {
  110.     if (NewSize<Len) Len=NewSize;
  111.   }
  112.  
  113. //.parse
  114.  
  115. void ByteVector::Assign(const ByteVector& BV)
  116.   {
  117.     if (P == BV.P) return;  // they're the same object
  118.     Len = BV.Len;
  119.     if (Len > Alloc)
  120.       {
  121.         FreeMemory();
  122.         Alloc = Len+Extra;
  123.         P = VecNew(Alloc);
  124.       }
  125.     BigMemCopy(P,BV.P,Len);
  126.   }
  127.  
  128. //.parse
  129.  
  130. ByteVector::ByteVector(const ByteVector& B)
  131.   {
  132.     Extra=DefaultVectorExtra;
  133.     Len = B.Len;
  134.     Alloc = Extra+Len;
  135.     P = VecNew(Alloc);
  136.     BigMemCopy(P,B.P,Len);
  137.   }
  138.  
  139. //.parse
  140.  
  141. ByteVector::ByteVector(void* B, long Length)
  142.   {
  143.     Extra=DefaultVectorExtra;
  144.     Len = Length;
  145.     Alloc = Extra+Len;
  146.     P = VecNew(Alloc);
  147.     BigMemCopy(P,B,Len);
  148.   }
  149.  
  150. //.parse
  151.  
  152. void ByteVector::WriteTo(File& F)
  153.   {
  154.     F.WriteThing(Len);
  155.     if (Len)
  156.       {
  157.         F.Write(P,size_t(Len));
  158.       }
  159.   }
  160.  
  161. //.parse
  162.  
  163. Byte* VecNew(long Quan)
  164.   {
  165.     Byte* B;
  166.     B=(Byte*)farmalloc(Quan);
  167.     if (B==NULL) FatalError("vector out of memory");
  168.     return B;
  169.   }
  170.  
  171. //.parse
  172.  
  173. Bool BigVectorJump=True;
  174.  
  175. void ByteVector::MinimizeMemory()
  176.   {
  177.     Bool Temp=BigVectorJump;
  178.     BigVectorJump=False;
  179.     ReNew(Len);
  180.     BigVectorJump=Temp;
  181.   }
  182.  
  183. //.parse
  184.  
  185. void ByteVector::ReNew(long NewCapacity)  // replaces ANSI-C realloc
  186.   {
  187.     void* NewP=farrealloc(P,NewCapacity);
  188.     if (NewP==NULL)
  189.       {
  190.         Byte huge* X=(Byte huge*)farmalloc(NewCapacity);
  191.         if (X==NULL)
  192.             FatalError("vector out of memory: "+Str(NewCapacity));
  193.         BigMemCopy(X,P,Len);
  194.         farfree(P);
  195.         P=X;
  196.       }
  197.     else P=(Byte*)NewP;
  198.     Alloc=NewCapacity;
  199.   }
  200.  
  201. long ByteVector::ReAlloc(long NewCapacity)
  202.   {
  203.     if (NewCapacity < Len) NewCapacity = Len;
  204.     if (Alloc != NewCapacity) ReNew(NewCapacity);
  205.     return Alloc;
  206.   }
  207.  
  208. //.parse
  209.  
  210. void ByteVector::Insert(const ByteVector& B,long Index)
  211.   {
  212.     *this=Before(Index)+B+From(Index);
  213.   }
  214.  
  215. void ByteVector::Insert(Byte B,long Index)
  216.   {
  217.     *this=Before(Index)+B+From(Index);
  218.   }
  219.  
  220. void ByteVector::Clear(Byte B)
  221.   {
  222.     long I;
  223.     For(I,Alloc) *PtrInc(P,I)=B;  // other funcs depend on clearing to alloc
  224.   }
  225.  
  226. //.parse
  227.  
  228. #define ParenGuts if (I>=Len) return 0; return (*PtrInc(P,I));
  229.  
  230. Byte ByteVector::At(long I) {ParenGuts}
  231. Byte ByteVector::operator()(long I) {ParenGuts}
  232.  
  233. void ByteVector::FreeMemory()
  234.   {
  235.     farfree(P);
  236.   }
  237.  
  238. ByteVector::ByteVector()
  239.   {
  240.     Extra=DefaultVectorExtra;
  241.     Len = 0;
  242.     Alloc = Extra;
  243.     P = VecNew(Alloc);
  244.   }
  245.  
  246. //.parse
  247.  
  248. ByteVector::ByteVector(int B)
  249.   {
  250.     Extra=DefaultVectorExtra;
  251.     Len = 1;
  252.     Alloc = Extra+1;
  253.     P = VecNew(Alloc);
  254.     P[0]=Byte(B);
  255.   }
  256.  
  257. //.parse
  258.  
  259. ByteVector::ByteVector(int B1,int B2)
  260.   {
  261.     Extra=DefaultVectorExtra;
  262.     Len = 2;
  263.     Alloc = Extra+Len;
  264.     P = VecNew(Alloc);
  265.     P[0]=Byte(B1);
  266.     P[1]=Byte(B2);
  267.   }
  268.  
  269. //.parse
  270.  
  271. ByteVector::ByteVector(int B1,int B2,int B3)
  272.   {
  273.     Extra=DefaultVectorExtra;
  274.     Len = 3;
  275.     Alloc = Extra+Len;
  276.     P = VecNew(Alloc);
  277.     P[0]=Byte(B1);
  278.     P[1]=Byte(B2);
  279.     P[2]=Byte(B3);
  280.   }
  281.  
  282. //.parse
  283.  
  284. ByteVector::ByteVector(int B1,int B2,int B3,int B4)
  285.   {
  286.     Extra=DefaultVectorExtra;
  287.     Len = 4;
  288.     Alloc = Extra+Len;
  289.     P = VecNew(Alloc);
  290.     P[0]=Byte(B1);
  291.     P[1]=Byte(B2);
  292.     P[2]=Byte(B3);
  293.     P[3]=Byte(B4);
  294.   }
  295.  
  296. //.parse
  297.  
  298. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5)
  299.   {
  300.     Extra=DefaultVectorExtra;
  301.     Len = 5;
  302.     Alloc = Extra+Len;
  303.     P = VecNew(Alloc);
  304.     P[0]=Byte(B1);
  305.     P[1]=Byte(B2);
  306.     P[2]=Byte(B3);
  307.     P[3]=Byte(B4);
  308.     P[4]=Byte(B5);
  309.   }
  310.  
  311. //.parse
  312.  
  313. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5,int B6)
  314.   {
  315.     Extra=DefaultVectorExtra;
  316.     Len = 6;
  317.     Alloc = Extra+Len;
  318.     P = VecNew(Alloc);
  319.     P[0]=Byte(B1);
  320.     P[1]=Byte(B2);
  321.     P[2]=Byte(B3);
  322.     P[3]=Byte(B4);
  323.     P[4]=Byte(B5);
  324.     P[5]=Byte(B6);
  325.   }
  326.  
  327. //.parse
  328.  
  329. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5,int B6,int B7)
  330.   {
  331.     Extra=DefaultVectorExtra;
  332.     Len = 7;
  333.     Alloc = Extra+Len;
  334.     P = VecNew(Alloc);
  335.     P[0]=Byte(B1);
  336.     P[1]=Byte(B2);
  337.     P[2]=Byte(B3);
  338.     P[3]=Byte(B4);
  339.     P[4]=Byte(B5);
  340.     P[5]=Byte(B6);
  341.     P[6]=Byte(B7);
  342.   }
  343.  
  344. //.parse
  345.  
  346. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5,int B6,int B7,int B8)
  347.   {
  348.     Extra=DefaultVectorExtra;
  349.     Len = 8;
  350.     Alloc = Extra+Len;
  351.     P = VecNew(Alloc);
  352.     P[0]=Byte(B1);
  353.     P[1]=Byte(B2);
  354.     P[2]=Byte(B3);
  355.     P[3]=Byte(B4);
  356.     P[4]=Byte(B5);
  357.     P[5]=Byte(B6);
  358.     P[6]=Byte(B7);
  359.     P[7]=Byte(B8);
  360.   }
  361.  
  362. //.parse
  363.  
  364. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5,int B6,int B7,
  365.     int B8,int B9)
  366.   {
  367.     Extra=DefaultVectorExtra;
  368.     Len = 9;
  369.     Alloc = Extra+Len;
  370.     P = VecNew(Alloc);
  371.     P[0]=Byte(B1);
  372.     P[1]=Byte(B2);
  373.     P[2]=Byte(B3);
  374.     P[3]=Byte(B4);
  375.     P[4]=Byte(B5);
  376.     P[5]=Byte(B6);
  377.     P[6]=Byte(B7);
  378.     P[7]=Byte(B8);
  379.     P[8]=Byte(B9);
  380.   }
  381.  
  382. //.parse
  383.  
  384. void ByteVector::CopyTo(void* Dest, long Bytes)
  385.   {
  386.     if (Bytes>Len) Bytes=Len;
  387.     BigMemCopy(Dest,P,Bytes);
  388.   }
  389.  
  390. void ByteVector::CopyFrom(void* Source, long Bytes)
  391.   {
  392.     if (Alloc<Bytes)
  393.       {
  394.         FreeMemory();
  395.         Alloc=Bytes+Extra;
  396.         P=VecNew(Alloc);
  397.       }
  398.     Len=Bytes;
  399.     BigMemCopy(P,Source,Bytes);
  400.   }
  401.  
  402. //.parse
  403.  
  404. void ByteVector::operator+=(const ByteVector& BV)
  405.   {
  406.     if (Alloc <= Len + BV.Len) ReAlloc(Len+BV.Len+Extra);
  407.     BigMemCopy(PtrInc(P,Len),BV.P,BV.Len);
  408.     Len += BV.Len;
  409.   }
  410.  
  411. ByteVector operator+(Byte B, const ByteVector& BV)
  412.   {
  413.     ByteVector V(B);
  414.     V+=BV;
  415.     return V;
  416.   }
  417.  
  418. //.parse
  419.  
  420. ByteVector ByteVector::operator+(const ByteVector& B)
  421.   {
  422.     ByteVector BV=*this;
  423.     BV+=B;
  424.     return BV;
  425.   }
  426.  
  427. ByteVector ByteVector::operator+(Byte B)
  428.   {
  429.     ByteVector BV=*this;
  430.     BV+=B;
  431.     return BV;
  432.   }
  433.  
  434. //.parse
  435.  
  436. ByteVector ByteVector::operator()(long Index, long Length)
  437.   {
  438.     ByteVector B;
  439.     if ((Index>=Len)||(Length==0)) return B;
  440.     else if (Index+Length>Len) Length=Len-Index;
  441.     B.ReAlloc(Length+Extra);
  442.     B.Len=Length;
  443.     BigMemCopy(B.P,PtrInc(P,Index),Length);
  444.     return B;
  445.   }
  446.  
  447. ByteVector ByteVector::At(long Index, long Length)
  448.   {
  449.     ByteVector B=operator()(Index,Length);
  450.     return B;
  451.   }
  452.  
  453. //.parse
  454.  
  455. Byte huge& ByteVector::Ref(long Index)
  456.   {
  457.     if (Index>=Alloc)
  458.       {
  459.         ReNew(Index+Extra);
  460.         Len=Index+1;
  461.       }
  462.     else if (Index>=Len) Len=Index+1;
  463.     Byte huge* BP=PtrInc(P,Index);
  464.     return *BP;
  465.   }
  466.  
  467.